package io.bdmc.modules.bss.service.impl;

import io.bdmc.common.annotation.DataFilter;
import io.bdmc.common.utils.MyPage;
import io.bdmc.modules.bss.entity.FibreCablePoint;
import io.bdmc.modules.bss.entity.FibreFibre;
import io.bdmc.modules.bss.mapper.FibreFibreMapper;
import io.bdmc.modules.bss.model.LabelValue;
import io.bdmc.modules.bss.service.IFibreCablePointService;
import io.bdmc.modules.bss.service.IFibreFibreService;

import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 光缆 服务实现类
 * </p>
 *
 * @author ping
 * @since 2020-07-02
 */
@Service
public class FibreFibreServiceImpl extends ServiceImpl<FibreFibreMapper, FibreFibre> implements IFibreFibreService {
    @Autowired
    FibreFibreMapper _fibrefibreMapper;

    @Autowired
    IFibreCablePointService _fibrePointSvs;

    @DataFilter(tableAlias = "d")
    @Override
    public MyPage<FibreFibre> mySelectPage(MyPage<FibreFibre> myPage) {
        return _fibrefibreMapper.mySelectPage(myPage);
    }

    @Override
    public List<HashMap<String, Object>> ztree(int deptId) {

        return _fibrefibreMapper.ztree(deptId);
    }

    @Transactional
    @Override
    public Boolean removeFibreAndPoints(int fibreId) {
        boolean result = false;
        result = _fibrePointSvs.remove(new QueryWrapper<FibreCablePoint>().eq("fibre_id", fibreId));
        result = removeById(fibreId);
        return result;
    }

    @Transactional
    @Override
    public Boolean insertFibreAndPoints(FibreFibre fibre, List<FibreCablePoint> points) {
        boolean result = false;
        result = fibre.insert();
        for (FibreCablePoint fibreCablePoint : points) {
            fibreCablePoint.setFibreId(fibre.getFibreId());
        }
        result = _fibrePointSvs.saveBatch(points);
        return result;
    }

    @Override
    public List<LabelValue> combo(int deptId) {

        return _fibrefibreMapper.combo(deptId);
    }

    @Override
    public List<FibreFibre> listByDeptId(int deptId) {
        return _fibrefibreMapper.listByDeptId(deptId);
    }

    @Override
    public HashMap<String, Object> fibreLengthSum(Long deptId) {
        return _fibrefibreMapper.fibreLengthSum(deptId);
    }
}
